const Set = require('../template')

function testSetPerformance() {
  // 1. 大量单层级属性设置性能
  console.log(
    "1. 大量单层级属性设置性能:",
    (() => {
      const obj = {}
      const iterations = 10000

      const start = performance.now()
      for (let i = 0; i < iterations; i++) {
        Set.set(obj, `key${i}`, i)
      }
      const end = performance.now()

      return (end - start) < 50 && obj[`key${iterations - 1}`] === iterations - 1
    })()
  )

  // 2. 大量嵌套层级属性设置性能
  console.log(
    "2. 大量嵌套层级属性设置性能:",
    (() => {
      const obj = { a: { b: {} } }
      const iterations = 5000

      const start = performance.now()
      for (let i = 0; i < iterations; i++) {
        Set.set(obj, `a.b.key${i}`, i)
      }
      const end = performance.now()

      return (end - start) < 40 && obj.a.b[`key${iterations - 1}`] === iterations - 1
    })()
  )

  // 3. setDeep 深度限制性能
  console.log(
    "3. setDeep 深度限制性能:",
    (() => {
      const obj = {}
      const iterations = 3000
      const maxDepth = 3

      const start = performance.now()
      for (let i = 0; i < iterations; i++) {
        Set.setDeep(obj, `x.y.z.key${i}`, i, maxDepth)
      }
      const end = performance.now()

      return (end - start) < 35 && obj.x.y.z[`key${iterations - 1}`] === iterations - 1
    })()
  )

  // 4. setWithCondition 条件设置性能
  console.log(
    "4. setWithCondition 条件设置性能:",
    (() => {
      const obj = { base: 10 }
      const iterations = 8000
      const condition = (val) => val > 5

      const start = performance.now()
      for (let i = 0; i < iterations; i++) {
        Set.setWithCondition(obj, `key${i}`, i, condition)
      }
      const end = performance.now()

      return (end - start) < 45 && obj[`key${iterations - 1}`] === iterations - 1
    })()
  )

  // 5. setMultiple 多路径批量设置性能
  console.log(
    "5. setMultiple 多路径批量设置性能:",
    (() => {
      const obj = {}
      const batchSize = 200
      const batches = 50
      const value = 'test'

      const start = performance.now()
      for (let i = 0; i < batches; i++) {
        const paths = Array.from({ length: batchSize }, (_, j) => `path${i}_${j}`)
        Set.setMultiple(obj, paths, value)
      }
      const end = performance.now()

      return (end - start) < 30 && obj[`path${batches - 1}_${batchSize - 1}`] === value
    })()
  )

}

// 执行性能测试
console.log("=== Set 性能测试 ===")
testSetPerformance()
console.log("=== Set 性能测试完成 ===")
